<!--
  * 物流报表-司机物流统计
  *
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-card size="small" :bordered="false" :hoverable="false" class="query-form-wrapper smart-margin-bottom10 smart-radius-2">
    <a-form>
      <a-row :gutter="[8, 10]">
        <a-col :span="5.5">
          <a-form-item label="单据日期" class="smart-margin-bottom0">
            <custom-range-picker v-model:value="queryForm.create_time" />
          </a-form-item>
        </a-col>
        <a-col :span="4">
          <a-form-item label="司机" class="smart-margin-bottom0">
            <a-input v-model:value.trim="queryForm.name" placeholder="司机" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="5">
          <a-button-group>
            <a-button type="primary" html-type="submit" @click="onSearch">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true" class="query-table-wrapper">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">
        <a-button @click="onExport" type="primary" v-privilege="'driver:sign:report'">
          <template #icon>
            <ExportOutlined />
          </template>
          导出
        </a-button>
      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.ERP.GOODS" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->
    <div class="table-wrapper">
      <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="driver_id" bordered :scroll="{ x: 'max-content', y: tableScrollY }" :pagination="false" :showSorterTooltip="false" :loading="tableLoading" :row-selection="{ fixed: true }">
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell>总计</a-table-summary-cell>
              <a-table-summary-cell />
              <a-table-summary-cell>
                <a-typography-text type="danger">{{ total_amount.total_delivery_count }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text type="danger">{{ total_amount.total_store_count }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text type="danger">{{ total_amount.total_delivery_num }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text type="danger">{{ total_amount.total_sign_num }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text type="danger">{{ total_amount.total_delivery_value }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text type="danger">{{ Number(total_amount.total_receivable_amount).toFixed(2) }}</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text type="danger">{{ Number(total_amount.total_received_amount).toFixed(2) }}</a-typography-text>
              </a-table-summary-cell>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </a-table>
    </div>

    <div class="smart-query-table-page query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS" :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData" :show-total="() => `共${total}条`" />
    </div>
  </a-card>
</template>
<script setup lang="ts">
import CustomRangePicker from '@/components/business/custom-range-picker/index.vue'
import { orderSignReportApi } from '@/api/business/report/report-api'
import TableOperator from '@/components/support/table-operator/index.vue'
import { useQuery } from '@/hooks/web/use-query-hook'
import { PAGE_SIZE_OPTIONS, DEFAULT_TIME_RANGE } from '@/constants/system/common-const'
import { TABLE_ID_CONST } from '@/constants/support/table-id-const'
import { useResize } from '@/hooks/web/use-resize-hook'

// ----------------------------- 计算表格高度 -----------------------------
const { tableScrollY } = useResize()

// ---------------------------- 表格列 ----------------------------

const columns = ref([
  {
    title: '司机',
    dataIndex: ['driver', 'name']
  },
  {
    title: '配送订单',
    dataIndex: 'delivery_count'
  },
  {
    title: '门店数量',
    dataIndex: 'store_count'
  },
  {
    title: '配送数量',
    dataIndex: 'delivery_num'
  },
  {
    title: '签收数量',
    dataIndex: 'sign_num'
  },
  {
    title: '配送货值',
    dataIndex: 'delivery_value'
  },
  {
    title: '应收金额',
    dataIndex: 'receivable_amount'
  },
  {
    title: '实收金额',
    dataIndex: 'received_amount'
  }
])

// ---------------------------- 查询数据表单和方法 ----------------------------
const total_amount = ref({
  total_delivery_count: 0,
  total_store_count: 0,
  total_delivery_num: 0,
  total_sign_num: 0,
  total_delivery_value: 0,
  total_receivable_amount: 0,
  total_received_amount: 0
})
const { queryForm, tableData, tableLoading, total, resetQuery, onSearch, queryData } = useQuery({
  fetchDataApi: async ({ page, limit, filter }) => {
    const data = {
      page,
      limit,
      ...filter
    }
    const res = await orderSignReportApi.driverSignReport(data)
    total_amount.value = {
      total_delivery_count: res.data.sum_data.total_delivery_count,
      total_store_count: res.data.sum_data.total_store_count,
      total_delivery_num: res.data.sum_data.total_delivery_num,
      total_sign_num: res.data.sum_data.total_sign_num,
      total_delivery_value: res.data.sum_data.total_delivery_value,
      total_receivable_amount: res.data.sum_data.total_receivable_amount,
      total_received_amount: res.data.sum_data.total_received_amount
    }
    return {
      list: res.data.list.data,
      total: res.data.list.total
    }
  },
  queryFormFilter: {
    name: '',
    type: 2,
    create_time: DEFAULT_TIME_RANGE.value
  },
  queryFormOp: {}
})
onMounted(queryData)

// ------------------------------- 导出和导入 ---------------------------------
// 导出
function onExport() {
  const data = {
    name: queryForm.value.name,
    create_time: queryForm.value.create_time,
    type: 2
  }
  orderSignReportApi.exportReport(data)
}
</script>
